//
//  TKIMEvaluateView.m
//  test2
//
//  Created by TryHone on 16/11/22.
//  Copyright © 2016年 TryHone. All rights reserved.
//

#import "TKIMEvaluateView.h"

@implementation TKIMEvaluateView

-(instancetype)init{
  
    self=[super init];
    if (self) {
        [self initUI];
    }
    
    return self;

}

-(void)initUI{
    [self addSubview:self.evaluateBtn];
    [self addSubview:self.cotentView];
    [self.cotentView addSubview:self.satisfiedEvaluateBtn];
    [self.cotentView addSubview:self.noSatisfiedEvaluateBtn];
    [self.cotentView addSubview:self.line];
    
}


-(void)layoutSubviews{
    
    
    
    [super layoutSubviews];
        
        if (_isSpred) {
            self.cotentView.hidden=NO;
            CGFloat widthItem=(168-1)/2;
            CGFloat height=44;
            self.cotentView.frame=CGRectMake(0, 0, 168, height);
            self.satisfiedEvaluateBtn.frame=CGRectMake(0, 0, widthItem, height);
            self.line.frame=CGRectMake(CGRectGetMaxX(self.satisfiedEvaluateBtn.frame), 0, 1, height);
            self.noSatisfiedEvaluateBtn.frame=CGRectMake(CGRectGetMaxX(self.line.frame), 0, widthItem, height);
            
            self.evaluateBtn.frame=CGRectMake(CGRectGetMaxX(self.cotentView.frame)+10, 0, 44, 44);
            
            
        }else{
            self.cotentView.hidden=YES;
            self.evaluateBtn.frame=CGRectMake(0, 0, 44, 44);
        }
    
   

}


-(void)btnClick:(UIButton*)btn{
    if (_block) {
        
        if (btn.tag==101) {//满意
            
            id data=[[TKIMInterfaceHelper sharedInstance] valueForKeyData:TKIMSDK_KEFU_USER_DATA];
            
            if (data) {
                NSDictionary* dict=(NSDictionary*)data;
                NSString * sessionid=dict[@"sessionid"];
                NSString * sid=dict[@"sid"];
                NSString * cid=dict[@"cid"];
                NSString * score=@"5";
                NSString * describe=@"满意";
                NSString * spuid=dict[@"toPuid"];
                NSString * cpuid=[TKIMSDKManager getLoginUserPuid];
                
                
                [TKIMInterfaceHelper evaluationSatisfaction:sessionid sid:sid cid:cid score:score describe:describe spuid:spuid cpuid:cpuid  msgId:@"" callback:^(NSInteger errorNumber, NSString *info) {
                    [[TKIMLayerUtils instance] showTip:info];
                    _block(btn.tag,btn,errorNumber);
                    
                }];
                
                
                
            }else{
            
                _block(btn.tag,btn,-1);

            }
            
            

        
        }else if (btn.tag==102){//不满意
        
            
            id data=[[TKIMInterfaceHelper sharedInstance] valueForKeyData:TKIMSDK_KEFU_USER_DATA];
            
            if (data) {
                NSDictionary* dict=(NSDictionary*)data;
                NSString * sessionid=dict[@"sessionid"];
                NSString * sid=dict[@"sid"];
                NSString * cid=dict[@"cid"];
                NSString * score=@"0";
                NSString * spuid=dict[@"toPuid"];
                 NSString * cpuid=[TKIMSDKManager getLoginUserPuid];
                
                NSString * describe=@"不满意";
                [TKIMInterfaceHelper evaluationSatisfaction:sessionid sid:sid cid:cid score:score describe:describe spuid:spuid cpuid:cpuid msgId:@"" callback:^(NSInteger errorNumber, NSString *info) {
                    [[TKIMLayerUtils instance] showTip:info];
                    _block(btn.tag,btn,errorNumber);
                    
                    
                }];
                
                
                
            }else{
            
                 _block(btn.tag,btn,-1);
            }
            
        }else{
        
            _block(btn.tag,btn,-1);
        
        }
        
        
     
    }
}

-(UIButton*)satisfiedEvaluateBtn{

    
    if (_satisfiedEvaluateBtn==nil) {
        _satisfiedEvaluateBtn=[[UIButton alloc] init];
        [_satisfiedEvaluateBtn setTitle:@"满意" forState:UIControlStateNormal];
        [_satisfiedEvaluateBtn setTitleColor:[TKUIHelper colorWithHexString:@"#2778be"] forState:UIControlStateNormal];
        [_satisfiedEvaluateBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_satisfiedEvaluateBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _satisfiedEvaluateBtn.tag=101;
    }
    return _satisfiedEvaluateBtn;

}
-(UIButton*)noSatisfiedEvaluateBtn{
    
    
    if (_noSatisfiedEvaluateBtn==nil) {
        _noSatisfiedEvaluateBtn=[[UIButton alloc] init];
        [_noSatisfiedEvaluateBtn setTitle:@"不满意" forState:UIControlStateNormal];
        [_noSatisfiedEvaluateBtn setTitleColor:[TKUIHelper colorWithHexString:@"#2778be"] forState:UIControlStateNormal];
        [_noSatisfiedEvaluateBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_noSatisfiedEvaluateBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _noSatisfiedEvaluateBtn.tag=102;
    }
    return _noSatisfiedEvaluateBtn;
    
}
-(UIButton*)evaluateBtn{
    if (_evaluateBtn==nil) {
        _evaluateBtn=[[UIButton alloc] init];
        [_evaluateBtn setTitle:@"服务\n评价" forState:UIControlStateNormal];
        [_evaluateBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _evaluateBtn.titleLabel.numberOfLines=0;
        _evaluateBtn.backgroundColor=[TKUIHelper colorWithHexString:@"#999999"];
        [_evaluateBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_evaluateBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        _evaluateBtn.layer.masksToBounds=YES;
        _evaluateBtn.layer.cornerRadius=5;
        _evaluateBtn.tag=103;
    }
    return _evaluateBtn;
}

-(UIView*)cotentView{
  
    if (_cotentView==nil) {
        _cotentView=[[UIView alloc] init];
        _cotentView.layer.masksToBounds=YES;
        _cotentView.layer.cornerRadius=5;
        _cotentView.layer.borderColor=[TKUIHelper colorWithHexString:@"#d9d9d9"].CGColor;
        _cotentView.backgroundColor=[TKUIHelper colorWithHexString:@"#e5e5e5"];
    }
    return _cotentView;

}

-(UIView*)line{

    if (_line==nil) {
        _line=[[UIView alloc] init];
        _line.backgroundColor=[TKUIHelper colorWithHexString:@"#d9d9d9"];
    }
    return _line;
}

@end
